package com.gxjtkyy.datadistributer.writer.rdbms.service;

import com.gxjtkyy.datadistributer.writer.rdbms.dao.QueryDao;
import com.gxjtkyy.datadistributer.writer.rdbms.entity.TargetInstitution;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class QueryService {
   private final QueryDao queryDao;

   @Cacheable(
      cacheNames = {"relativeInstitution"},
      key = "#root.method+'_'+#root.args[0]"
   )
   public Set<String> getRelativeStationIds(String orgaCode) {
      return this.getGeneralIds(this.queryDao.queryRelativeStations(orgaCode));
   }

   @Cacheable(
      cacheNames = {"relativeInstitution"},
      key = "#root.method+'_'+#root.args[0]"
   )
   public Set<String> getRelativeGantryIds(String orgaCode) {
      return this.getGeneralIds(this.queryDao.queryRelativeGantries(orgaCode));
   }

   private Set<String> getGeneralIds(List<TargetInstitution> targetInstitutions) {
      return (Set)targetInstitutions.stream().map(TargetInstitution::getGeneralId).filter(Objects::nonNull).collect(Collectors.toSet());
   }

   public QueryService(final QueryDao queryDao) {
      this.queryDao = queryDao;
   }
}
